package com.myapi.modeldemo.controller;

import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.myapi.modeldemo.entity.Asentence;
import com.myapi.modeldemo.mapper.AsentenceMapper;
import com.myapi.modeldemo.service.AsentenceService;
import lombok.extern.slf4j.Slf4j;
import modeldemo.model.User;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Random;

/**
 * @Description:
 * @date 2023/3/14 9:52
 * @Modified By :
 */
@RestController
@RequestMapping("/request")
@Slf4j
public class myapicontroller {

    @Resource
    private AsentenceService asentenceService;

    @Resource
    private AsentenceMapper asentenceMapper;

    @RequestMapping("/postone")
    public String Postrequest(@RequestParam String name){
        System.out.println("Postone接口正常调用");
        return "你好"+name;
    }

    @PostMapping("/postbody")
    public String Postrequest(@RequestBody User user, HttpServletRequest request){
        System.out.println("打招呼");
        return "你好" + user.getName();
    }

    @GetMapping("/translate")
    public String translate(@RequestParam(value = "txt") String txt){
        System.out.println("拿到的参数"+txt);
        String fanyi = null;
        String result = HttpRequest.get("https://api.vvhan.com/api/fy?text=" + txt)
                .execute().body();
        if (result != null) {
            JSONObject json1 = new JSONObject(result);
            fanyi = json1.getJSONObject("data").getStr("fanyi");
            System.out.println("翻译后：" + fanyi);
        } else {
            log.info("请求失败");
        }
        return fanyi;
    }

    @GetMapping("/randomImg")
    public String randomImg(){
        String Imge = null;
        String result = HttpRequest.get("https://api.vvhan.com/api/bing?type=json&rand=sj")
                .execute().body();
        if (result != null) {
            JSONObject json1 = new JSONObject(result);
            Imge = json1.getJSONObject("data").getStr("url");
            System.out.println("图片地址：" + Imge);
        } else {
            log.info("请求失败");
        }
        return Imge;
    }

    @GetMapping("/CodeImg")
    public String CodeImg(@RequestParam(value = "txt") String txt){
        String Imge = null;
        String result = HttpRequest.get("https://api.axtn.net/api/textqrcode?text="+txt)
                .execute().body();
        if (result != null) {
            JSONObject json1 = new JSONObject(result);
             Imge = json1.getStr("qrcode");
            System.out.println("二维码地址：" + Imge);
        } else {
            log.info("请求失败");
        }
        return Imge;
    }

    @GetMapping("/chatget")
    public String chatget(HttpServletRequest request,@RequestParam(value = "txt") String txt){
        String chat = null;
        System.out.println("拿到的请求头："+request.getHeader("accessKey"));
        String result = HttpRequest.get("https://api.ownthink.com/bot?userid="+request.getHeaders("accessKey")+"&spoken="+txt)
                .execute().body();
        if (result != null) {
            JSONObject json1 = new JSONObject(result);
            chat = json1.getJSONObject("data").getJSONObject("info").getStr("text");
        } else {
            log.info("请求失败");
        }
        return chat;
    }


    @GetMapping("/onesentence")
    public String onesentence(@RequestParam(value = "txt") String txt){
        String sentence = asentenceMapper.randomsentence(txt).getSentence();
        if (sentence == null){
            log.info("请求失败");
        }
        return sentence;
    }

}
